class Solution {
public:
    int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
    int n, m;
    vector<vector<char>> updateBoard(vector<vector<char>>& board,vector<int>& click) {
        n = board.size(), m = board[0].size();
        int i = click[0], j = click[1];
        if (board[i][j] == 'M') {
            board[i][j] = 'X';
            return board;
        }
        dfs(i, j, board);
        return board;
    }
    void dfs(int i, int j, vector<vector<char>>& board) {
        int count = 0;
        for (int k = 0; k < 8; k++) {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && y >= 0 && x < n && y < m && board[x][y] == 'M') {
                count++;
            }
        }
        if (count != 0) {
            board[i][j] = count + '0';
            return;
        } else {
            board[i][j] = 'B';
            for (int k = 0; k < 8; k++) {
                int x = i + dx[k], y = j + dy[k];
                if (x >= 0 && y >= 0 && x < n && y < m && board[x][y]=='E') {
                    board[x][y] = 'B';
                    dfs(x, y, board);
                }
            }
        }
    }
};